<?php
namespace app\index\controller;
use think\Cache;
use think\Request;
use app\common\model\Users;
/**
 * 前端用户登录
 * Class Login
 * @package app\index\controller
 */
class Login extends Base
{

    /**
     * 商家登录 跟当前登录的小程序用户绑定  商家和普通用户不是一个表  商家登录的时候 通过绑定关系获取商家信息（作为扩展信息）
     * 如果深圳不提供接口，那么商家登录就用手机验证码登陆 每次
     * 或者可以用手机验证码绑定商家的 微信 那么只有被绑定的微信才能够进行操作
     */
    public function login(Request $request)
    {
        $token = $request->param('token');
        $username = $request->param('username'); //商家账号
        $password = $request->param('password');
        if (empty($token)) {
            $this->error("非法操作");
        }

        $user = check_login($token);
        $condition['username'] = $username;
        $condition['password'] = $password;
        //$shop = Shop::where($condition)->find();
        //var_dump($shop);
        $shop = ['username' => '', 'shopname' => 'abc'];
        $user->shop = $shop;

        //返回少量前端可以用到的数据
        $this->success('登录成功', null, ['user' => []]);

    }






    /**
     * $data = [
     *   "avatarUrl" => "https://wx.qlogo.cn/mmopen/vi_32/MxBkVGOIzrBWHGqLllASqK6wdpiaMgjSY8OMewG6A3MyibfuagP06j4N5oiavB7tuSUohqjZiaIyDCNic82HbZRdMbQ/0",
     *   "city" => "Dongguan",
     *   "country" => "China",
     *   "gender" => 1,
     *   "language" => "zh_CN",
     *   "nickName" => "小吉",
     *   "province" => "Guangdong",
     *   "openid" => '123456',
     * ];
     * @param Request $request
     * @throws
     */
    public function updateUserInfo(Request $request)
    {
        $data = $request->post();

        $condition = array();
        $condition['openid'] = trim($this->token['openid']);
        $user = Users::where($condition)->find();
        if ($user) {
            $user->avatar_url = $data['avatarUrl'];
            $user->nickname = $data['nickName'];
            $user->avatar_url = $data['avatarUrl'];
            $user->city = $data['city'];
            $user->country = $data['country'];
            $user->gender = $data['gender'];
            $user->province = $data['province'];
            $user->update_time = time();

            $user->save();
            $this->success("更新成功",'',['mobile'=>$user->mobile]);
        }
        $this->error('更新失败','');
    }




    /**
     * 绑定微信号和用户手机号
     */
    public function bindWx()
    {

    }
}